package com.wh.wisdomsite.xbox.us.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.us.dao.UserMapper;
import com.wh.wisdomsite.xbox.us.dao.UserRoleMapper;
import com.wh.wisdomsite.xbox.us.entity.User;
import com.wh.wisdomsite.xbox.us.entity.UserRole;
import com.wh.wisdomsite.xbox.us.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 用户ServiceImpl
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.us.service.impl
 * @author: 丁奕
 * @date: 2018-08-03 15:42
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
@Service("userService")
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private static final Log logger = LogFactory.getLog(UserServiceImpl.class);

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public Map<String, Object> getMapById(Serializable id) {
        return userMapper.getMapById(id);
    }

    @Override
    public Page<Map<String, Object>> getMapsPage(Page page, Wrapper<User> wrapper) {
        SqlHelper.fillWrapper(page, wrapper);
        page.setRecords(this.baseMapper.getMapsPage(page, wrapper));
        return page;
    }

    /**
     * 根据用户ID先删除所有关联的用户角色
     * @param usId
     * @return
     */
    @Override
    public Integer deleteUserRoleByUserId(Serializable usId) {
        EntityWrapper<UserRole> ew = new EntityWrapper<>();
        ew.setEntity(new UserRole());
        ew.eq("usId",usId);
        return userRoleMapper.delete(ew);
    }

    /**
     * 验证登陆名称是否已经存在
     * @param loginName
     * @param oldName
     * @return
     */
    @Override
    public boolean loginNameIsExists(String loginName, String oldName) {
        //判断是否有原始值，有原始值则为修改, 且原始值跟新值不相同，才进行查询，否则直接返回false
        if(StringUtils.isNotEmpty(oldName) && loginName.equals(oldName)){
            return false;
        }
        //设置条件
        EntityWrapper<User> ew = new EntityWrapper<>();
        ew.setEntity(new User());
        ew.eq("loginName", loginName);
        return userMapper.selectCount(ew) > 0;
    }

    /**
     * 验证用户手机是否已存在
     * @param userPhone
     * @param oldPhone
     * @return
     */
    @Override
    public boolean userPhoneIsExists(String userPhone, String oldPhone) {
        //判断是否有原始值，有原始值则为修改, 且原始值跟新值不相同，才进行查询，否则直接返回false
        if(StringUtils.isNotEmpty(oldPhone) && userPhone.equals(oldPhone)){
            return false;
        }
        //设置条件
        EntityWrapper<User> ew = new EntityWrapper<>();
        ew.setEntity(new User());
        ew.eq("userPhone", userPhone);
        return userMapper.selectCount(ew) > 0;
    }

    @Override
    public List<User> selectUserListByProjectId(String projectId) {
        return userMapper.selectUserListByProjectId(projectId);
    }

    @Override
    public List<User> selectUserByRoleId(String roleId,String usId) {
        return userMapper.selectUserByRoleId(roleId,usId);
    }

    @Override
    public boolean deleteById(Serializable id) {
        //先删除关联的菜单中间表内容
        deleteUserRoleByUserId(id);
        return super.deleteById(id);
    }
}
